package com.squareup.timessquare;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.timessquare.CalendarPickerView.OnDateSelectedListener;
import com.squareup.timessquare.CalendarPickerView.SelectionMode;
import com.yksj.healthtalk.ui.R;


public class CalendarFragmentView extends Fragment {
	
	CalendarPickerView mPickerView;
	Calendar mCalendar;
	OnDateSelectedListener mSelectedListener;
	Collection<Date> mBusyList;
	Collection<Date> mIdleList;
	Date mSelectedDate;
	public void setSelectedListener(OnDateSelectedListener mSelectedListener) {
		this.mSelectedListener = mSelectedListener;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCalendar = Calendar.getInstance();
		mSelectedDate = mCalendar.getTime();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.calendar_fragment_layout, null);
		mPickerView = (CalendarPickerView)view.findViewById(R.id.calendar_view);
		mPickerView.setOnDateSelectedListener(new OnDateSelectedListener() {
			@Override
			public void onDateSelected(Date date) {
				if(mSelectedListener != null)mSelectedListener.onDateSelected(date);
			}
			@Override
			public void onDateClick(MonthCellDescriptor descriptor) {
				if(mSelectedListener != null)mSelectedListener.onDateClick(descriptor);
				mPickerView.selectDate(mSelectedDate = descriptor.getDate());
			}
			@Override
			public void onChangeMonth(int i) {
				if(i == -1){
					onPrevious();
				}else{
					onNext();
				}
				if(mSelectedListener != null)mSelectedListener.onChangeMonth(i);
			}
		});
		iniCalenderView();
		return view;
	}
	
	private void onNext(){
		mCalendar.add(Calendar.MONTH,1);
		mPickerView.init(mCalendar.getTime(),mCalendar.getTime()) //
        .inMode(SelectionMode.SINGLE)
        .withSelectedDate(mSelectedDate)
        .withStateDate(mBusyList, mIdleList);
	}
	
	public void onSelectDate(Date date){
		if(date != null)mPickerView.selectDate(date);
	}
	
	private void onPrevious(){
		mCalendar.add(Calendar.MONTH,-1);
		mPickerView.init(mCalendar.getTime(),mCalendar.getTime()) //
        .inMode(SelectionMode.SINGLE)
        .withSelectedDate(mSelectedDate)
		.withStateDate(mBusyList, mIdleList);
	}
	
	/**
	 * 改变状态显示空闲还是忙
	 * @param busyDates
	 * @param idleDates
	 */
	public void onUpdateState(Collection<Date> busyDates,Collection<Date> idleDates){
		this.mBusyList = busyDates;
		this.mIdleList = idleDates;
		if(mPickerView != null){
			mPickerView.init(mCalendar.getTime(),mCalendar.getTime()) //
	        .inMode(SelectionMode.SINGLE)
	        .withStateDate(mBusyList, mIdleList);
		}
	}
	
	public void initCalenderView(Calendar calendar){
		if(calendar != null){
			this.mCalendar = calendar;
			this.mSelectedDate = calendar.getTime();
			iniCalenderView();
		}
	}
	
	private void iniCalenderView(){
		/*Calendar calendar = Calendar.getInstance();
		mBusyList = new ArrayList<Date>();
		mBusyList.add(calendar.getTime());
		calendar.add(Calendar.DATE, 1);
		mBusyList.add(calendar.getTime());
		
		mIdleList = new ArrayList<Date>();
		calendar.add(Calendar.DATE,-2);
		mIdleList.add(calendar.getTime());
		calendar.add(Calendar.DATE,-1);
		mIdleList.add(calendar.getTime());*/
		
		mPickerView.init(mCalendar.getTime(),mCalendar.getTime()) //
		.inMode(SelectionMode.SINGLE)
		.withSelectedDate(mSelectedDate)
		.withStateDate(mBusyList, mIdleList);
	}
	
}
